/**
 * Created by Nie on 2015/12/10.
 */
app.config(function($routeProvider, $locationProvider) {
    $routeProvider.
    when('/cms/list', {
        templateUrl: 'cms/list.html',
        controller: 'cmsListController'
    }).
    when('/cms/edit/:id', {
        templateUrl: 'cms/edit.html',
        controller: 'cmsEditController'
    })

});

function cmsService( $http, $location ){

    return {

        edit : function( data,  recall ){

            baas_jz.call({
                'http': $http,
                'method': 'post',
                'data':data,
                'uri': 'cms/index/edit/'+data.id,
                'success': function(d) {
                    if(d.status == '1'){
                        if( typeof recall == 'function'){
                            recall( d );
                        }
                    }else{

                        if( d.description ){
                            alert(d.description );
                        }else{
                            alert( '网络原因，请求失败，请稍后重试' );
                        }

                    }
                }
            });
        },
        list : function( data, fn ){
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'cms/index/search/list',
                'data': data,
                'success': function(d) {
                    if( typeof fn == 'function'){
                        fn( d);
                    }
                }
            });


        },
        find_one : function( id,   fn ){
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'cms/index/find/' + id,
                'success': function(d) {
                    if( typeof fn == 'function'){
                        fn( d );
                    }
                }
            });
        },
        delete : function( id, fn ){
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'cms/index/delete/'+id,
                'success': function(d) {
                    if( typeof fn == 'function'){
                        fn( d );
                    }
                }
            });
        }

    };
}

app.factory('cmsService', ['$http','$location', cmsService]);

app.controller('cmsListController', ['$scope', '$routeParams','$window', '$location', '$http', 'cmsService', cmsListController]);
app.controller('cmsEditController', ['$scope','$rootScope', '$routeParams','$window', '$location', '$http', 'cmsService', cmsEditController]);

function cmsEditController( $scope, $rootScope,$routeParams, $window, $location, $http, cmsService  ){



    /*end*/
    $scope.id = $routeParams.id;
    if( $scope.id != 0 ){
        cmsService.find_one( $scope.id, function( json ){
            console.log(json);
            $scope.cms = json.res;
        });
    }

    $scope.edit = function( data ){
        if(!data || data.title==""){
            alert("标题不能为空");
            return ;
        }

        var manager=JSON.parse(localStorage.getItem(sessionStorage.getItem("who")+"manager"));
        data.company_code=manager.company_code;
        data.company_name=manager.company_name;
        data.id=$scope.id;
        data.type="备忘录";
        data.username=manager.manager_account;


        cmsService.edit(data,  function( json ){

            if( json.status ){
                $location.path('/cms/list');
            }
        });
    };



}


function cmsListController( $scope, $routeParams, $window, $location, $http, cmsService ){
    $scope.URL=WEB_URL;
    $scope.app_id = $routeParams.app_id;
    $scope.selectedPage = 1;
    $scope.search = {
        limit:20,
        page:1
    };
    var manager=JSON.parse(localStorage.getItem(sessionStorage.getItem("who")+"manager"));
    $scope.search.username=manager.manager_account;
    $scope.getList = function(){
        cmsService.list($scope.search, function( json ){
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    $scope.getList();

    $scope.searchCms = function(){
        $scope.search.page=1;
        $scope.search.start_time=angular.element("#start_time").val();
        $scope.search.end_time=angular.element("#end_time").val();
        cmsService.list($scope.search, function( json ){
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };


    /*分页*/
    $scope.changePage = function(p) {
        $scope.search.page = p;
        cmsService.list($scope.search, function(json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function(count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
            console.log(count);
        }
    };



    $scope.delete = function( id ){
        cmsService.delete( id, function( json ){
            if( json.status ){
                $scope.getList();
            }

        });

    }

}